package com.st.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>title: com.wfx.interceptor</p>
 * author zhuximing
 * description:
 */
public class UserInfoInterceptor implements HandlerInterceptor {


   private static ThreadLocal<String> local = new ThreadLocal<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取用户id
        String userId = request.getHeader("userId");

        //将userId放入到threadLocal
        local.set(userId);


        return true;
    }


    //请求结束
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        local.remove();
    }



    public static String  getUserInfo(){
        return  local.get();
    }
}